package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.common.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysAlarm;
import com.ruoyi.system.service.ISysAlarmService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 告警信息Controller
 * 
 * @author ruoyi
 * @date 2021-04-27
 */
@Controller
@RequestMapping("/system/alarm")
public class SysAlarmController extends BaseController
{
    private String prefix = "system/alarm";

    @Autowired
    private ISysAlarmService sysAlarmService;

    @RequiresPermissions("system:alarm:view")
    @GetMapping()
    public String alarm()
    {
        return prefix + "/alarm";
    }

    /**
     * 查询告警信息列表
     */
    @RequiresPermissions("system:alarm:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysAlarm sysAlarm)
    {
        startPage();
        List<SysAlarm> list = sysAlarmService.selectSysAlarmList(sysAlarm);
        return getDataTable(list);
    }

    /**
     * 导出告警信息列表
     */
    @RequiresPermissions("system:alarm:export")
    @Log(title = "告警信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysAlarm sysAlarm)
    {
        List<SysAlarm> list = sysAlarmService.selectSysAlarmList(sysAlarm);
        ExcelUtil<SysAlarm> util = new ExcelUtil<SysAlarm>(SysAlarm.class);
        return util.exportExcel(list, "告警信息数据");
    }

    /**
     * 新增告警信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存告警信息
     */
    @RequiresPermissions("system:alarm:add")
    @Log(title = "告警信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysAlarm sysAlarm)
    {
        System.out.println(sysAlarm.toString());
        return toAjax(sysAlarmService.insertSysAlarm(sysAlarm));
    }

    /**
     * 修改告警信息
     */
    @GetMapping("/edit/{alarmId}")
    public String edit(@PathVariable("alarmId") String alarmId, ModelMap mmap)
    {
        SysAlarm sysAlarm = sysAlarmService.selectSysAlarmById(alarmId);
        mmap.put("sysAlarm", sysAlarm);
        return prefix + "/edit";
    }

    /**
     * 修改保存告警信息
     */
    @RequiresPermissions("system:alarm:edit")
    @Log(title = "告警信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysAlarm sysAlarm)
    {
        //往redi里面(put) 添加保存deviceId对应的command信息

        return toAjax(sysAlarmService.updateSysAlarm(sysAlarm));
    }

    /**
     * 删除告警信息
     */
    @RequiresPermissions("system:alarm:remove")
    @Log(title = "告警信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysAlarmService.deleteSysAlarmByIds(ids));
    }

}
